using System;
using System.Globalization;
using System.Linq;
using System.Web;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Entities;

namespace AB.ServiceDirectory.Design.SupportingMaterials
{
    public partial class AddASupportingMaterials : System.Web.UI.Page
    {
        /// <summary>
        /// Gets or sets the organization id.
        /// </summary>
        /// <value>
        /// The organization id.
        /// </value>
        public int OrganizationId
        {
            get
            {
                if (ViewState["OrganizationId"] != null)
                {
                    return (int)ViewState["OrganizationId"];
                }
                return -1;
            }

            set { ViewState["OrganizationId"] = value; }
        }

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        /// <summary>
        /// Loads the data.
        /// </summary>
        private void LoadData()
        {
            int organisationId;
            if (int.TryParse(Request.Params["organisationId"], out organisationId))
            {
                if (OrganizationBO.GetOrganizationById(organisationId) != null)
                {
                    OrganizationId = organisationId;
                    DropDownListType.DataSource = BO.ASupportingMaterialBO.LoadAllType().ToList();
                    DropDownListType.DataBind();
                    TextBoxAddedBy.Text = HttpContext.Current.User.Identity.Name;
                    TextBoxAddedDate.Text = String.Format(CultureInfo.CurrentCulture, "{0:dd/MM/yyyy}", DateTime.Now);
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the ButtonSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            if (Session["userId"] != null)
            {
                var supportingMaterial = new SupportingMaterial();
                supportingMaterial.Url = TextBoxURL.Text;

                // fix cung  vi chua co session
                supportingMaterial.UserId = (int)Session["userId"];
                supportingMaterial.Description = TextBoxDescription.Text;
                supportingMaterial.AddedDate = DateTime.Now;
                supportingMaterial.IsActive = true;
                supportingMaterial.OrganizationId = OrganizationId;
                supportingMaterial.Type = DropDownListType.SelectedValue;
                bool insertSupportingMaterial = ASupportingMaterialBO.InsertSupportingMaterial(supportingMaterial);
                if (insertSupportingMaterial)
                {
                    Response.Redirect(string.Format(CultureInfo.CurrentCulture, "~/Organisation/OrganisationDetails02.aspx?id={0}#DetailsPanel05", OrganizationId));
                }
                else
                {
                    Response.Write("<script>alert('Insert To Database Failed!')</script>");
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the ButtonBack control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format(CultureInfo.CurrentCulture, "~/Organisation/OrganisationDetails.aspx?id={0}#DetailsPanel05", OrganizationId));
        }
    }
}